package hflyeer.o2o.biz.service.impl;

import java.io.File;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hflyeer.o2o.biz.exceptions.ShopOperatorExection;
import hflyeer.o2o.biz.service.ShopService;
import hflyeer.o2o.enums.ShopStateEnum;
import hflyeer.o2o.model.dao.ShopDao;
import hflyeer.o2o.model.dto.ShopExecution;
import hflyeer.o2o.model.entity.Shop;
import hflyeer.o2o.util.ImageUtils;
import hflyeer.o2o.util.PageUtils;
import hflyeer.o2o.util.PathUtils;

@Service("ShopService")
public class ShopServiceImpl implements ShopService {

	@Autowired
	private ShopDao shopDao;

	@Override
	@Transactional
	public ShopExecution addShop(Shop shop, File img) {
		if (shop == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}
		if (shop.getOwnerId() == 0) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP_OWNER);
		}
		if (shop.getShopName() == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP_NAME);
		}
		try {
			shop.setCreateTime(new Date());
			shop.setLastEditTime(new Date());
			shop.setPriority(0);
			shop.setEnableStatus(0);
			// 添加店铺
			int res = shopDao.insert(shop);
			if (res <= 0) {
				throw new ShopOperatorExection("店铺添加失败");
			} else {
				// 更新店铺图片地址
				addShopImg(shop, img);
				res = shopDao.update(shop);
				if (res <= 0) {
					throw new ShopOperatorExection("店铺图片地址更新失败");
				}
			}

		} catch (Exception e) {
			throw new ShopOperatorExection("addShop error:" + e.getMessage());
		}

		return new ShopExecution(ShopStateEnum.SUCCESS, shop);
	}

	// 添加店铺图片
	private void addShopImg(Shop shop, File img) {
		String shopDir = PathUtils.getShopImagePath(shop.getShopId());
		String ShopImgPath = ImageUtils.thumbnails(img, shopDir);
		shop.setShopImg(ShopImgPath);
	}

	@Override
	@Transactional
	public ShopExecution editShop(Shop shop, File img) {
		if (shop == null) {
			return new ShopExecution(ShopStateEnum.NULL_SHOP);
		}
		// 更新图片
		if (img != null) {
			// 删除原来的图片
			String shopDir = PathUtils.getShopImagePath(shop.getShopId());
			String imgPath = shop.getShopImg();
			File file = new File(shopDir + imgPath);
			if (file.exists()) {
				file.delete();
			}
			// 更新新图片
			addShopImg(shop, img);
		}
		shop.setLastEditTime(new Date());
		int res = shopDao.update(shop);
		if (res <= 0) {
			return new ShopExecution(ShopStateEnum.UPDATE_FAIL);
		}
		// 更新成功
		return new ShopExecution(ShopStateEnum.SUCCESS, shop);
	}

	@Override
	public ShopExecution getShopList(Shop shopCondition, int pageNum, int pageSize) {
		ShopExecution shopExecution = new ShopExecution();
		try {
			// 符合条件的记录总数
			shopExecution.setCount(shopDao.countShop(shopCondition));
			shopExecution.setShopList(shopDao.queryForShopByCondition(shopCondition,
					PageUtils.pageNumToRowIndex(pageNum, pageSize), pageSize));
		} catch (Exception e) {
			shopExecution = new ShopExecution(ShopStateEnum.INNER_ERROR);
		}

		return shopExecution;
	}

}
